package com.xbongbong.pro.service.stage.impl;

import com.xbongbong.paas.domain.entity.PaasStageEntity;
import com.xbongbong.paas.model.PaasStageModel;
import com.xbongbong.paas.pojo.StageFindPojo;
import com.xbongbong.pro.service.stage.AbstractStageFindService;
import com.xbongbong.pro.service.stage.StageFindStrategy;
import com.xbongbong.pro.service.stage.factory.StageFindFactory;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * PaaS阶段查询策略
 * @author zcp
 * @version v1.0
 * @since v1.0
 */
@Component
public class PaasStageStrategyImpl extends AbstractStageFindService implements StageFindStrategy {
    @Resource
    private PaasStageModel paasStageModel;

    @Override
    @PostConstruct
    public void register() {
        StageFindFactory.STAGE_MAP.put(XbbRefTypeEnum.PAAS.getCode(), this);
    }

    @Override
    public PaasStageEntity getByCode(String corpid, Long formId, String code) {
        return paasStageModel.getByCode(corpid, formId, code);
    }

    @Override
    public List<PaasStageEntity> findStageList(String corpid, StageFindPojo stageFindPojo) {
        Map<String, Object> param = commonHelp.packageFilter(corpid, stageFindPojo);
        //sort从大到小排序--如果sort相等，默认按照id
        param.put(ParameterConstant.ORDER_BY_STR, "sort desc, id asc");
        List<PaasStageEntity> saleStageList = paasStageModel.findEntitys(param);
        return saleStageList;
    }

    @Override
    public List<? extends PaasStageEntity> findStageListSpecial(String corpid, StageFindPojo stageFindPojo) {
        //paas的不处理
        return findStageList(corpid, stageFindPojo);
    }

    @Override
    public Set<String> findStageCodeList(String corpid, StageFindPojo stageFindPojo) {
        Map<String, Object> param = commonHelp.packageFilter(corpid, stageFindPojo);
        //只查id, code
        param.put(ParameterConstant.COLUMNS, "id, code");
        List<PaasStageEntity> saleStageList = paasStageModel.findEntitys(param);
        Set<String> saleStageCodeIn = saleStageList.stream().map(entity -> entity.getCode()).collect(Collectors.toSet());
        //防空
        saleStageCodeIn.add("-1");
        return saleStageCodeIn;
    }
}
